import BaseItemView from "../../../cfw/mvc/BaseItemView";
import GiftItemModel from "../model/GiftItemModel";
import BaseItemModel from "../../../cfw/mvc/BaseItemModel";
import { ItemState } from "../../../cfw/tools/Define";
import ModuleManager, { ModuleID } from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";


const { ccclass, property } = cc._decorator;

@ccclass
export default class GiftItemVIew extends BaseItemView {

	@property({ type: cc.Sprite, displayName: "icon$VSprite" })
	icon$VSprite: cc.Sprite = null;

	@property({ type: cc.Sprite, displayName: "box$VSprite" })
	box$VSprite: cc.Sprite = null;

	@property({ type: cc.Sprite, displayName: "ad$VSprite" })
	ad$VSprite: cc.Sprite = null;

	@property({ type: cc.Button, displayName: "click$VButton" })
	click$VButton: cc.Button = null;

	protected model: GiftItemModel;


	onLoad() {
	}

	addListener() {
		this.eventProxy.on(BaseItemModel.UPDATE_STATE, this.refresh, this)
	}

	refresh() {
		let state = this.model.getState()
		switch (state) {
			case ItemState.NOT_GET:
				this.icon$VSprite.node.active = false;
				this.ad$VSprite.node.active = true;
				this.box$VSprite.node.active = true;
				this.setSprite(this.box$VSprite, this.model.getBox())
				break;
			case ItemState.CAN_GET:
				this.ad$VSprite.node.active = false;
				this.icon$VSprite.node.active = false;
				this.box$VSprite.node.active = true;
				this.setSprite(this.box$VSprite, this.model.getBox())
				break;
			case ItemState.GOT:
				this.ad$VSprite.node.active = false;
				this.box$VSprite.node.active = false;
				this.icon$VSprite.node.active = true;
				this.setSprite(this.icon$VSprite, this.model.getIcon())
				break;
		}
	}

	setSprite(sp: cc.Sprite, image: string) {
		ModuleManager.getLoader(ModuleID.GIFT).loadRes(image, ResType.SpriteFrame, (err, res: ResItem) => {
			if (err || !cc.isValid(this.node)) {
				return;
			}
			sp.spriteFrame = res.getRes()
		})
	}

	onDestroy() {
		super.onDestroy()
	}

	onclick$VButtonClick() {

		this.controller.clickItem(this.model)
		if(this.model.getState() == ItemState.GOT){
			cc.Tween.stopAllByTarget(this.node)
			cc.tween(this.node).to(0.2,{scale:1.1}).to(0.2,{scale:1.0}).start()
		}
	}



}